//
//  NSObject+Reachability.m
//  DS_PJ_01
//
//  Created by admin on 16/8/17.
//  Copyright © 2016年 zhiqiyun. All rights reserved.
//

#import "DSReachabilityState.h"

@interface DSReachabilityState ()

@property(nonatomic,strong)currentReachabilityStatusBlock statusBlock;

@end

@implementation DSReachabilityState

-(DSReachabilityState *)initReachability:(currentReachabilityStatusBlock)statusBlock
{
    self = [super init];
    if (self) {
        
        self.statusBlock = statusBlock;
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(networkStateChange) name:kReachabilityChangedNotification object:nil];
        Reachability *_shareReach = [Reachability reachabilityForInternetConnection];
        [_shareReach startNotifier];
        //获得当前网络状态
        NetworkStatus status = [_shareReach currentReachabilityStatus];
        if (status == NotReachable) {
            NSAssert(status == NotReachable, @"无网络连接,请检查网络设置");
        }
    }
    
    return self;
}

-(void)networkStateChange
{
    // 2.检测手机是否能上网络(WIFI\3G\2.5G)
    Reachability *conn = [Reachability reachabilityForInternetConnection];
    
    // 3.判断网络状态
    NetworkStatus status = [conn currentReachabilityStatus];
    NSString *info = nil;
    
    if (status == ReachableViaWiFi) {
        // 有wifi
        info = @"有wifi" ;
        
    } else if (status != NotReachable) {
        // 没有使用wifi, 使用手机自带网络进行上网
        info = @"使用手机自带网络进行上网" ;
    } else { // 没有网络
        info = @"网络连接中断,请检查网络设置" ;
    }
    self.statusBlock(status,info);
}


@end
